<?php
/**
 * 
 * @param unknown $username
 * @param unknown $email
 * @param unknown $firstname
 * @param unknown $lastname
 * @param unknown $role
 * @return string
 */
function seitcm_add_employer($username,$email,$firstname,$lastname,$role)
{
	$userdata = array (
			'ID' => '',
			'user_url' => '',
			'user_login' => $username,
			'user_email' => $email,
			'first_name' => $firstname,
			'last_name' => $lastname,
			'user_pass' => $username,
			'role' => $role,
	);
	$id = wp_insert_user($userdata);
	return $id;
}
/**
 * 
 * @param unknown $seitcm_role
 * @param number $number
 * @param number $offset
 * @return multitype:
 */
function seitcm_get_list_employers($seitcm_role,$number=10,$offset=0)
{
	if(!($seitcm_role=='seitcm_teacher' || $seitcm_role=='seitcm_counselor' || $seitcm_role=='seitcm_manager'))
	{
		$sql = 'SELECT `user_id` FROM `wp_usermeta` WHERE `meta_key` LIKE \'wp_capabilities\' 
				AND (`meta_value` LIKE \'%seitcm_manager%\'  OR `meta_value` LIKE \'%seitcm_counselor%\' 
				OR `meta_value` LIKE \'%seitcm_teacher%\' )';
		$aid = array();
		global $wpdb;
		foreach($wpdb->get_results($sql) as $id)
		{
			array_push($aid, $id->user_id);
		}
		if(count($aid)==0) return $aid;
		$args  = array(
				'include' => $aid,
				'number' => $number,
				'offset' => $offset,
				'orderby' => 'ID',
				'order' => 'DESC',
		);
		$wp_user_query = new WP_User_Query($args);
		return  $wp_user_query->get_results();
	}
	else {
		$args  = array(
		'role' => $seitcm_role,
				'number' => $number,
				'offset' => $offset,
				'orderby' => 'ID',
				'order' => 'DESC'
		);
		$wp_user_query = new WP_User_Query($args);
		return  $wp_user_query->get_results();
	}
}
/**
 * 
 * @param unknown $id
 * @return multitype:
 */
function seitcm_get_single_employers($id)
{
	$args  = array(
			'search' => $id,
			'search_columns' => array('ID')
	);
	$wp_user_query = new WP_User_Query($args);
	return array_shift($wp_user_query->get_results());
}
/**
 * 
 * @param unknown $role
 * @return multitype:
 */
function seitcm_get_count_employers($seitcm_role)
{
	$sql = 'SELECT `user_id` FROM `wp_usermeta` WHERE `meta_key` LIKE \'wp_capabilities\'
				AND (`meta_value` LIKE \'%seitcm_manager%\'  OR `meta_value` LIKE \'%seitcm_counselor%\'
				OR `meta_value` LIKE \'%seitcm_teacher%\' )';
	$aid = array();
	global $wpdb;
	foreach($wpdb->get_results($sql) as $id)
	{
		array_push($aid, $id->user_id);
	}
	if(count($aid)==0) return $aid;
			
	if(!($seitcm_role=='seitcm_teacher' || $seitcm_role=='seitcm_counselor' || $seitcm_role=='seitcm_manager'))
	{
		$args  = array(
				'include' => $aid,
		);
		$wp_user_query = new WP_User_Query($args);
		return  $wp_user_query->get_total();
	}
	else {
		$args  = array(
			'role' => $seitcm_role,
		);
		$wp_user_query = new WP_User_Query($args);
		return  $wp_user_query->get_total();
	}
}
/**
 * 
 * @param unknown $user_ID
 */
function seitcm_check_exist_teacher_id($user_ID){

	$list_teacher = seitcm_get_list_employers('seitcm_teacher');
	foreach ($list_teacher as $teacher)
	{
		if($teacher->ID == $user_ID)
		{
			return true;
			break;
		}
	}
	return false;
}
/**
 * 
 * @param unknown $id
 * @param unknown $email
 * @param unknown $firstname
 * @param unknown $lastname
 * @param string $pass
 * @return Ambigous <number, WP_Error>
 */
function seitcm_update_employer($id,$email,$firstname,$lastname,$pass=''){
	if($pass=='') {
		return wp_update_user(array( 'ID' => $id, 'user_url' => $email, 'first_name' => $firstname, 'last_name' => $lastname));
	}
	else {
		return wp_update_user(array( 'ID' => $id, 'user_url' => $email, 'first_name' => $firstname, 'last_name' => $lastname, 'user_pass' => $pass));
	}
}
?>